package project.ia04.agents.gui;

import java.io.IOException;
import java.util.Map;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;


import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import project.ia04.utils.Data;

public class StaffActionBehaviour extends CyclicBehaviour {

	@Override
	public void action() {
		MessageTemplate template = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM),
													MessageTemplate.MatchConversationId(Data.STAFFACTION));
		ACLMessage message = myAgent.receive(template);
		if(message != null){
			System.out.println("XAgent --> StaffActionBehaviour (MSG)");
			((XAgent)myAgent).staffAction(getStaffNameDeserialize(message.getContent()),
										getMsgDeserialize(message.getContent()));
		}
		else{
			template = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM),
					MessageTemplate.MatchConversationId(Data.NEWLVL));
			message = myAgent.receive(template);
			if(message != null){
				System.out.println("XAgent --> StaffActionBehaviour (LVLUP)");
				((XAgent)myAgent).staffLvlUp(getStaffNameDeserialize(message.getContent()),
				getLvlDeserialize(message.getContent()));
			}
			else{
				template = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM),
						MessageTemplate.MatchConversationId(Data.STAMINA));
				message = myAgent.receive(template);
				if(message != null){
					System.out.println("XAgent --> StaffActionBehaviour (STAMINA)");
					((XAgent)myAgent).staffStaminaChange(getStaffNameDeserialize(message.getContent()),
							getStaminaDeserialize(message.getContent()));
				}
				else
					block();
			}
		}
	}
	
	private int getStaminaDeserialize(String content) {
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map;
		int stamina = 0;
		try {
			map = mapper.readValue(content,Map.class);
			stamina = (Integer)map.get(Data.STAMINA);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stamina;
	}

	private int getLvlDeserialize(String content){
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map;
		int staffLvl = 0;
		try {
			map = mapper.readValue(content,Map.class);
			staffLvl = (Integer) map.get(Data.NEWLVL);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return staffLvl;
	}

	private String getMsgDeserialize(String content) {
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map;
		String staffMsg = null;
		try {
			map = mapper.readValue(content,Map.class);
			staffMsg = (String) map.get(Data.STAFFMSG);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return staffMsg;
	}

	private String getStaffNameDeserialize(String content) {
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map;
		String staffName = null;
		try {
			map = mapper.readValue(content,Map.class);
			staffName = (String) map.get(Data.STAFFNAME);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return staffName;
	}

}
